
using Youz.Common.DBUtility;
using Youz.Common.Entity;
using Youz.Common.Helper;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace Youz.Common.DAL
{
    /// <summary>
    /// CMS_Article Data Class
    /// </summary>
    public partial class CMS_ArticleDAL : BaseDAL
    {
        private DbHelperSQL DbHelperSQL = new DbHelperSQL();
        public string TableName = "CMS_Article";

        public CMS_ArticleDAL()
        { }

        #region  成员方法
        /// <summary>
        ///  新增一条数据
        /// </summary>
        public bool Add(CMS_Article model)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("insert into [CMS_Article](");
            strSql.Append("[Title],[Content],[Creator],[CreateTime],[IsPublish],[Publisher],[PublishTime],[Cover],[MenuID],[Reads]");
            strSql.Append(") values (");
            strSql.Append("@Title,@Content,@Creator,@CreateTime,@IsPublish,@Publisher,@PublishTime,@Cover,@MenuID,@Reads");
            strSql.Append(")");

            SqlParameter[] parameters = {
                  
                    new SqlParameter("@Title", model.Title),
                    new SqlParameter("@Content", model.Content),
                    new SqlParameter("@Creator", model.Creator),
                    new SqlParameter("@CreateTime", model.CreateTime),
                    new SqlParameter("@IsPublish", model.IsPublish),
                    new SqlParameter("@Publisher", model.Publisher),
                    new SqlParameter("@PublishTime", model.PublishTime),
                    new SqlParameter("@Cover", model.Cover),
                    new SqlParameter("@MenuID", model.MenuID),
                    new SqlParameter("@Reads", model.Reads)
            };

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        ///  保存一条数据
        /// </summary>
        public bool Update(CMS_Article model)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("update [CMS_Article] set ");
            strSql.Append("[Title]=@Title,[Content]=@Content,[Creator]=@Creator,[CreateTime]=@CreateTime,[IsPublish]=@IsPublish,[Publisher]=@Publisher,[PublishTime]=@PublishTime,[Cover]=@Cover,[MenuID]=@MenuID,[Reads]=@Reads");
            strSql.Append(" where [ID]=@ID");

            SqlParameter[] parameters = {
                  
                    new SqlParameter("@ID", model.ID),
                    new SqlParameter("@Title", model.Title),
                    new SqlParameter("@Content", model.Content),
                    new SqlParameter("@Creator", model.Creator),
                    new SqlParameter("@CreateTime", model.CreateTime),
                    new SqlParameter("@IsPublish", model.IsPublish),
                    new SqlParameter("@Publisher", model.Publisher),
                    new SqlParameter("@PublishTime", model.PublishTime),
                    new SqlParameter("@Cover", model.Cover),
                    new SqlParameter("@MenuID", model.MenuID),
                    new SqlParameter("@Reads", model.Reads)
            };

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        ///</summary>
        public bool Delete(Int64 ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from [CMS_Article] ");
            strSql.Append(" where [ID]=@ID");

            SqlParameter[] parameters = {
			
                new SqlParameter("@ID", ID)             
              
            };

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CMS_Article Get(Int64 ID)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("select  top 1 * from [CMS_Article] ");
            strSql.Append(" where [ID]=@ID");

            SqlParameter[] parameters = {
			
                new SqlParameter("ID", ID)             
              
            };

            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return Get(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CMS_Article Get(DataRow row)
        {
            if (row == null) return null;

            CMS_Article model = new CMS_Article();


            if (row["ID"] != DBNull.Value)
            {
                model.ID = (Int64)row["ID"];
            }

            if (row["Title"] != DBNull.Value)
            {
                model.Title = (String)row["Title"];
            }

            if (row["Content"] != DBNull.Value)
            {
                model.Content = (String)row["Content"];
            }

            if (row["Creator"] != DBNull.Value)
            {
                model.Creator = (String)row["Creator"];
            }

            if (row["CreateTime"] != DBNull.Value)
            {
                model.CreateTime = (DateTime)row["CreateTime"];
            }

            if (row["IsPublish"] != DBNull.Value)
            {
                model.IsPublish = (Boolean)row["IsPublish"];
            }

            if (row["Publisher"] != DBNull.Value)
            {
                model.Publisher = (String)row["Publisher"];
            }

            if (row["PublishTime"] != DBNull.Value)
            {
                model.PublishTime = (DateTime)row["PublishTime"];
            }

            if (row["Cover"] != DBNull.Value)
            {
                model.Cover = (String)row["Cover"];
            }

            if (row["MenuID"] != DBNull.Value)
            {
                model.MenuID = (int)row["MenuID"];
            }

            if (row["Reads"] != DBNull.Value)
            {
                model.Reads = (int)row["Reads"];
            }

            if (row["IsBaidu"] != DBNull.Value)
            {
                model.IsBaidu = (Boolean)row["IsBaidu"];
            }

            if (row["BaiduedOn"] != DBNull.Value)
            {
                model.BaiduedOn = (DateTime)row["BaiduedOn"];
            }

            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet Gets(string sqlWhere)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("select * from [CMS_Article] ");
            if (sqlWhere.Trim() != "")
            {
                strSql.Append(" where " + sqlWhere);
            }

            return DbHelperSQL.Query(strSql.ToString());
        }

        public DataTable Gets(int Top, string sqlWhere, string showColumn, params SqlParameter[] cmdParms)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            if (string.IsNullOrEmpty(showColumn))
            {
                strSql.Append(" * ");
            }
            else
            {
                strSql.AppendFormat(" {0} ", showColumn);
            }

            strSql.Append(" FROM [CMS_Article] ");

            if (sqlWhere.Trim() != "")
            {
                strSql.Append(" where " + sqlWhere);
            }
            strSql.Append(" order by PublishTime desc");

            return CommonHelper.GetDateTable(DbHelperSQL.Query(strSql.ToString(), cmdParms));
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string sqlWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM [CMS_Article] ");
            if (sqlWhere.Trim() != "")
            {
                strSql.Append(" where " + sqlWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 获得数据列表分页
        /// </summary>
        public DataTable Gets(string sqlWhere, int pageindex, int pagesize, out int total, params SqlParameter[] cmdParms)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append(" SELECT * FROM [CMS_Article]  ");
            if (sqlWhere.Trim() != "")
            {
                strSql.Append(" where " + sqlWhere);
            }

            int count = 0;
            DataTable dt = DbHelperSQL.GetPager(pageindex, pagesize, "" + strSql + "", "order by CreateTime desc ", out count, cmdParms);
            total = count;
            return dt;
        }
        #endregion  成员方法

        public DataSet GetHots(int menuID)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.AppendFormat("select top 5 * from [CMS_Article] where IsPublish=1 and MenuID={0} order by Reads desc", menuID);

            return DbHelperSQL.Query(strSql.ToString());
        }

        public DataSet GetNews(int menuID)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.AppendFormat("select top 3 * from [CMS_Article] where IsPublish=1 and MenuID={0} order by Reads asc", menuID);

            return DbHelperSQL.Query(strSql.ToString());
        }

        public DataSet GetNearby(long id)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.AppendFormat(@"select * from (select top 1 * from [dbo].[CMS_Article] where IsPublish=1 and ID<{0} order by id desc) a
                                select * from (select top 1 * from [dbo].[CMS_Article] where IsPublish=1 and ID>{0} order by id asc) b", id);

            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得未提交到百度的链接
        /// </summary>
        /// <returns></returns>
        public DataSet GetUnsubmitUrl()
        {
            return DbHelperSQL.Query(@"select ID,MenuID,IsBaidu from [dbo].[CMS_Article] where IsPublish=1 and isnull(IsBaidu,0)=0
                                        update [dbo].[CMS_Article] set IsBaidu=1,BaiduedOn=getdate() where IsPublish=1 and isnull(IsBaidu,0)=0");
        }
    }
}


